package com.shawn.baseappbygroovydsl.ui.base

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.shawn.baseappbygroovydsl.MainActivity
import com.shawn.baseappbygroovydsl.R
import com.shawn.baseappbygroovydsl.ui.views.SecondFragment

/**
 * 描述：
 * 作者： shawn
 * 时间： 2024/8/510:18
 */
abstract class FragmentBase<VVBB : ViewBinding>(layoutRes: Int) : Fragment(layoutRes) {
    private var isLazyLoaded = false
    protected lateinit var mBind: VVBB
    protected val mHandler = Handler(Looper.getMainLooper())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBind = getBind(view)
        if (!isLazyLoad()) {
            lazyInLoad()
        }
    }

    override fun onResume() {
        super.onResume()
        if (isLazyLoad()) {
            lazyInLoad()
        }
        statusBar()
    }
    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }

    private fun lazyInLoad() {
        if (!isLazyLoaded) {
            isLazyLoaded = true
            lazyLoad()
            initClick()
            getData()
        }
    }

    open fun isLazyLoad() = false

    abstract fun statusBar()
    abstract fun lazyLoad()
    abstract fun initClick()
    abstract fun getData()
    abstract fun getBind(view: View): VVBB

    abstract fun onKeyCodeBack(): Boolean

    fun hideSubFragBySelf(frag: Fragment) {
        parentFragment?.childFragmentManager?.beginTransaction()?.hide(frag)
            ?.commitAllowingStateLoss()
    }

    fun showSubFrag(tag: String, frag: Fragment, containerId: Int, enterAnimation: Int = -1) {
        val existFrag = childFragmentManager.findFragmentByTag(tag)
        val transaction = childFragmentManager.beginTransaction()
        if (enterAnimation != -1) {
            transaction.setCustomAnimations(enterAnimation, 0)
        }
        if (null != existFrag) {
            transaction
                .show(existFrag)
                .commitAllowingStateLoss()
        } else {
            transaction
                .add(containerId, frag, tag)
                .commitAllowingStateLoss()
        }
    }

    fun removeSubFragBySelf(frag: Fragment) {
        val transaction = parentFragment?.childFragmentManager?.beginTransaction()
        if (frag is SecondFragment) {
            transaction?.setCustomAnimations(0, R.anim.anim_drama_exit)
        }
        transaction?.remove(frag)
            ?.commitAllowingStateLoss()
    }

    fun showFullScreenFrag(frag: Fragment) {
        if (context is MainActivity) {
            (context as MainActivity).showFullScreenFrag(frag)
        }
    }

    fun showFullScreenFrag(frag: Fragment, enterAnimation: Int = -1) {
        if (context is MainActivity) {
            (context as MainActivity).showFullScreenFrag(frag, enterAnimation)
        }
    }

    fun removeFullScreenFrag(tag: String) {
        if (context is MainActivity) {
            (context as MainActivity).removeFullScreenFrag(tag)
        }
    }
}